package com.link.play.fit.ai.ui.exercise

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.widget.addTextChangedListener
import com.google.gson.Gson
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.ExerciseBean
import com.link.play.fit.ai.bean.RecentlyEatenData
import com.link.play.fit.ai.databinding.AcDescribeBinding
import com.link.play.fit.ai.ui.main.MainAc
import com.link.play.fit.ai.utils.ClipboardUtils.copyText
import com.link.play.fit.ai.utils.EventUtils


class DescribeAc : BaseActivity<AcDescribeBinding>(AcDescribeBinding::inflate) {

    val exerciseBean = ExerciseBean(3, describe = "")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val dataR = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("data", RecentlyEatenData::class.java)
        } else {
            @Suppress("DEPRECATION")
            (intent.getSerializableExtra("data") as RecentlyEatenData?)
        }
        dataR?.let {
            exerciseBean.describe = dataR.describe
            exerciseBean.modify = 1
        }
        if (exerciseBean.describe.isNotEmpty()) {
            binding.edit.setText(exerciseBean.describe)
            binding.tvBtn.isEnabled = true
        }

        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvSample.text = getText(
            arrayOf(
                R.string.swam_laps_in_the_pool_for_30_mins_legs_exhausted,
                R.string.brisk_walked_and_climbed_stairs_for_45_mins_legs_slightly_tired,
                R.string.hiked_a_steep_trail_for_1_hour_lungs_and_legs_burned,
                R.string.cycled_hills_for_50_mins_quads_on_fire,
                R.string.hiit_for_20_mins_5_10_intensity,
                R.string.ran_5_miles_in_40_mins_sweating_heavily,
                R.string.leg_strength_training_for_35_mins_9_10_intensity,
                R.string.dance_cardio_for_45_mins_4_10_intensity,
                R.string.ran_10_miles_in_1_5_hours_legs_heavy,
                R.string.stair_climbing_for_25_mins_thighs_burning,
                R.string.lifted_weights_for_40_mins_drenched_in_sweat,
                R.string.played_basketball_for_1_hour_fairly_intense,
                R.string.parasailing_for_30_mins_core_and_arms_strained,
                R.string.yoga_for_60_mins_stretched_and_relaxed,
                R.string.rock_climbing_for_45_mins_not_too_tiring,
                R.string.pilates_for_50_mins_core_and_flexibility_improved,
                R.string.rowed_for_30_mins_arms_and_back_tired,
                R.string.boxing_for_35_mins_arms_very_sore,
                R.string.paddleboarding_for_1_hour_2_10_intensity,
                R.string.stairmaster_for_30_mins_legs_pushed_to_their_limit,
                R.string.stretching_for_20_mins_relaxed_and_limber,
                R.string.leisure_bike_ride_for_30_mins_felt_refreshed,
                R.string.walked_dog_for_25_mins_light_exercise,
                R.string.morning_yoga_for_15_mins_relaxing,
                R.string.relaxing_swim_for_20_mins_felt_refreshed,
            ).random()
        )

        binding.tvSample.setOnClickListener {
            copyText("sample", binding.tvSample.text.toString())
            Toast.makeText(this, "copy sample", Toast.LENGTH_SHORT).show()
        }
        binding.edit.addTextChangedListener {
            if (it.isNullOrEmpty()) {
                binding.tvBtn.isEnabled = false
            } else {
                binding.tvBtn.isEnabled = true
                exerciseBean.describe = it.toString()
            }
        }
        binding.clMore.setOnClickListener {
            binding.deleteText.visibility = View.VISIBLE
            binding.deleteText.setOnClickListener {
                exerciseBean.modify = -1
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra("Exercise", exerciseBean)
                })
                finish()
            }
        }
        binding.tvBtn.setOnClickListener {

            if (exerciseBean.modify == 0) {
                EventUtils.event("log_exercise_describe")
                goToAc(MainAc::class.java) {
                    putExtra("Exercise", Gson().toJson(exerciseBean))
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                }
            } else {
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra("Exercise", exerciseBean)
                })
            }
            finish()

        }
    }

}